package com.googlecode.cswish.util.lucene;

import java.io.Serializable;

import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.util.AttributeImpl;

public class UserIdAttributeImpl extends AttributeImpl implements
		UserIdAttribute, Cloneable, Serializable {
	
	private long id = 0;
	private long messageId = 0;

	/**
	 * <p/>
	 * 
	 * Get the bitset for any bits that have been set. This is completely
	 * distinct from {@link TypeAttribute#type()}, although they do share
	 * similar purposes. The flags can be used to encode information about the
	 * token for use by other {@link org.apache.lucene.analysis.TokenFilter}s.
	 * 
	 * 
	 * @return The bits
	 */
	public long getUserId() {
		return id;
	}

	/**
	 * @see #getUserId()
	 */
	public void setUserId(long flags) {
		this.id = flags;
	}

	@Override
	public void clear() {
		id = 0;
	}

	@Override
	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}

		if (other instanceof UserIdAttributeImpl) {
			return ((UserIdAttributeImpl) other).id == id
					&& ((UserIdAttributeImpl) other).messageId == messageId;
		}

		return false;
	}

	@Override
	public int hashCode() {
		return messageId == 0 ? (int)id : (int)messageId;
	}

	@Override
	public void copyTo(AttributeImpl target) {
		UserIdAttribute t = (UserIdAttribute) target;
		t.setUserId(id);
		t.setMessageId(messageId);
	}

	@Override
	public long getMessageId() {
		return messageId;
	}

	@Override
	public void setMessageId(long messageId) {
		this.messageId = messageId;
	}
}
